package humynn.sbd
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.geom.Point;
	
	import humynn.engine.Control;
	import humynn.engine.Input;
	import humynn.engine.Renderer;

	/**
	 * The health bar control for the HUD.
	 */
	public class HealthBar extends Control
	{
		private var _frameFile:String;
		private var _healthScale:Point;
		private var _bugMan:StinkBugManager;
		
		public function HealthBar(bugMan:StinkBugManager)
		{
			super();
			
			_imageFile = "healthbar.png";
			_frameFile = "healthframe.png";
			_healthScale = new Point(100, 1);
			
			_bugMan = bugMan;
		}
		
		/**
		 * Gets the percent of health remaining.
		 * 
		 * @returns The persent remaining.
		 */
		public function get health():int
		{
			return _healthScale.x;
		}
		
		/**
		 * Resets the health bar to full.
		 */
		public function reset():void
		{
			_healthScale.x = 100;
		}
		
		/**
		 * Overrides the update function to adjust the health bar if it's needed.
		 * 
		 * @param input The user input.
		 */
		public override function update(input:Input):void
		{
			_healthScale.x -= (60 / 1000) * _bugMan.stoppedCount * Settings.bugDamage;
			
			if(_healthScale.x < 0)
			{
				_healthScale.x = 0;
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		/**
		 * Draws the health bar and the frame.
		 * 
		 * @param rendereer The renderer to draw to.
		 */
		public override function draw(renderer:Renderer):void
		{
			renderer.draw(_frameFile, 0, 0, _rect.x, _rect.y);
			renderer.draw(_imageFile, 0, 0, _rect.x + 2, _rect.y + 2, _healthScale);
		}
	}
}